//
//  registViewController.m
//  theCoupleLive
//
//  Created by USER on 2016/12/2.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "registViewController.h"
//#import "liveAPI.h"
#import "coupleLiveAPI.h"
#import "setInfoViewController.h"
@interface registViewController ()
@property(nonatomic,copy)NSString * randomNumber;
@property(nonatomic,assign)int flog;
@end

@implementation registViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    self.btn_sendMesg.layer.masksToBounds=YES;
    self.btn_sendMesg.layer.cornerRadius=3;
    self.btn_regist.layer.masksToBounds=YES;
    self.btn_regist.layer.cornerRadius=8;
    self.pwd.secureTextEntry=YES;
    self.flog=0;
    [self.btn_tick addTarget:self action:@selector(agreeTick) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_sendMesg addTarget:self action:@selector(addtimer) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 添加计时器
-(void)addtimer{
    if (self.telephonenum.text.length != 11) {
        [self alert:@"手机号码不正确"];
    }
    else {
         [self sendSMS];
    }
}

#pragma mark 发送短信
-(void)sendSMS
{
    int num = (arc4random() % 10000);
    self.randomNumber = [NSString stringWithFormat:@"%.4d", num];
    
    [coupleLiveAPI getNoteWithMobile:self.telephonenum.text content:[NSString stringWithFormat:@"您的验证码是：%@。请不要把验证码泄露给其他人。",self.randomNumber] success:^(id response) {
    }];
    // 多线程计时器
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.btn_sendMesg.userInteractionEnabled = YES;
                [self.btn_sendMesg setTitle:@"获取验证码" forState:UIControlStateNormal];
                [self.btn_sendMesg setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                //重新获随机数
                int num = (arc4random() % 10000);
                self.randomNumber = [NSString stringWithFormat:@"%.4d", num];
                NSLog(@"%@",self.randomNumber);
            });
        }else{
            int seconds = timeout % 60;
            __block NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.btn_sendMesg.userInteractionEnabled = NO;
                [self.btn_sendMesg setTitle:[NSString stringWithFormat:@"%@s后重发",strTime] forState:UIControlStateNormal];
                [self.btn_sendMesg setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

#pragma mark 同意协议
-(void)agreeTick{
    if (self.flog==0) {
        [self.btn_tick setImage:[UIImage imageNamed:@"tyibianse"] forState:UIControlStateNormal];
        self.flog=1;
    }
    else{
        [self.btn_tick setImage:[UIImage imageNamed:@"tyi"] forState:UIControlStateNormal];
        self.flog=0;
    }
}

#pragma mark 注册
- (IBAction)regist:(UIButton *)sender {
    if (self.pwd.text.length>=6 && self.pwd.text.length<=10) {
        if ([self.messageCheck.text isEqualToString:self.randomNumber]) {
            if (self.flog==1) {
                setInfoViewController *setInfo=[[setInfoViewController alloc]initWithNibName:@"setInfoViewController" bundle:[NSBundle mainBundle]];
                setInfo.teleNum=self.telephonenum.text;
                setInfo.password=self.pwd.text;
                [self.navigationController pushViewController:setInfo animated:YES];
            }
            else{
                [self alert:@"请同意服务协议"];
            }
        }
        else{
            [self alert:@"验证码出错，请重新获取！"];
        }
    }
    else{
        [self alert:@"账号或密码错误"];
    }
}

#pragma mark  alther弹窗
-(void)alert:(NSString *)mes{
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示:" message:mes preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    
    [controller addAction:action1];
    
    [self presentViewController:controller animated:YES completion:nil];
    
}

#pragma mark 点击空白位置键盘隐藏
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.telephonenum resignFirstResponder];
    [self.pwd resignFirstResponder];
    [self.messageCheck resignFirstResponder];
    //textView键盘隐藏
    //[textView resignFirstResponder];
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
